import { Coupon } from './coupon-model.js';
var coupon = new Coupon();
var util = require('../../utils/util.js');
Page({
  data: {
    id:'',
    showShare: false, //分享弹窗
    type:'get'  //如果用户未领取类型为get,用户用户已领取类型为use
  },

  onLoad: function (options) {
    var type = options.type ? options.type : 'get';
    this.data.id = options.id ? options.id : '';
    var config = wx.getStorageSync('config');
    this.data.site_name = config[2].value;
    if(type != 'get'){
      this.setData({type});
    }
    if (this.data.id){
      this.getCouponInfo();
    }
  },
  //获取优惠券信息
  getCouponInfo:function(){
    var that = this;
    var args = {
      id: this.data.id
    }
    that.setData({
      loadingHidden: false
    })
    coupon.getDetail(args, (res) => {
      res.msg.coupon.use_end = util.formatTime(res.msg.coupon.use_end, "Y-M-D")
      this.setData({
        item:res.msg
      })
    });
  },
  //立即使用优惠券
  use:function(){
    var product_id = this.data.item.coupon.product_limiter_id;
    if (product_id > 0){
      wx:wx.redirectTo({
        url: '../product/product?id=' + product_id,
      })
    }else{
      wx.switchTab({
        url: '/pages/home/home',
      })
    }
  },
  //转赠弹窗
  zhuanzeng: function (e) {
    this.setData({
      showShare: true
    })
  },
  hideModal: function (e) {
    this.setData({
      showShare: false
    })
  },
  //分享卡券
  onShareAppMessage: function (res) {
    var that = this;
    if (res.from != 'button') {
      var name = that.data.site_name ? that.data.site_name : '首页';
      return {
        title: name,
        path: '/pages/home/home',
        //imageUrl: this.data.data.author_header,
      }
    } else {
      var args = {
        item_id: this.data.id
      }
      coupon.setShare(args, (res) => {
        if (res.status == 0) {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        }
      });
      return {
        title: '我送您一张卡券，请收下吧',
        path: '/pages/coupon/friend?id=' + this.data.id,
      }
    }
  },
})